//
//  YPhotoCell.m
//  TestZoomPhoto
//
//  Created by yull on 14/12/6.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "YPhotoCell.h"
#import "UIImageView+WebCache.h"
#import "SDWebImageManager.h"

@implementation YPhotoCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.scrollView = [[TapScrollView alloc] initWithFrame:self.contentView.bounds];
        self.scrollView.delegate = self;
        self.scrollView.backgroundColor = [UIColor clearColor];
        self.scrollView.contentSize = self.scrollView.frame.size;
        self.scrollView.minimumZoomScale = 0.8f;
        self.scrollView.maximumZoomScale = 1.5f;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.showsVerticalScrollIndicator = NO;
        
        self.thumbImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, 200)];
        self.thumbImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        self.thumbImageView.center = CGPointMake(self.scrollView.frame.size.width/2, self.scrollView.frame.size.height/2);
        [self.scrollView addSubview:self.thumbImageView];
        
        self.imageView = [[UIImageView alloc] initWithFrame:self.scrollView.bounds];
        [self.scrollView addSubview:self.imageView];
        
        [self.contentView addSubview:self.scrollView];
        
        self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault];
        self.progressView.frame = CGRectMake(80, self.thumbImageView.bottom + 20, CGRectGetWidth(self.contentView.frame) - 160, 20);
        self.progressView.progressTintColor = [UIColor blueColor];
        [self.contentView addSubview:self.progressView];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapGesture:)];
        [self addGestureRecognizer:tapGesture];
    }
    return self;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageView;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView
{
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)?(scrollView.bounds.size.width - scrollView.contentSize.width)/2 : 0.0;
    
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)?(scrollView.bounds.size.height - scrollView.contentSize.height)/2 : 0.0;
    
    self.imageView.center = CGPointMake(scrollView.contentSize.width/2 + offsetX,scrollView.contentSize.height/2 + offsetY);
}

- (void)drawCellWithPhoto:(YPhoto *)photo
{
    self.scrollView.zoomScale = 1.0f;
    
    self.progressView.hidden = YES;

    CGFloat width = CGRectGetWidth(self.scrollView.bounds);
    CGFloat height = CGRectGetHeight(self.scrollView.bounds);
    
    if (photo.image) {
        self.imageView.image = photo.image;
        self.imageView.size = [YPhoto getImageSize:photo.image];
        self.imageView.center = CGPointMake(width/2, height/2);
    }else {
        if (photo.hasBig) {
            self.thumbImageView.image = nil;
            
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:photo.imageUrl] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                self.imageView.size = [YPhoto getImageSize:image];
                self.imageView.center = CGPointMake(width/2, height/2);
            }];
        }else {
            self.progressView.hidden = NO;
            
            [self.thumbImageView sd_setImageWithURL:[NSURL URLWithString:photo.thumbUrl] placeholderImage:[UIImage imageNamed:@"chat_img_default_gray"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                self.thumbImageView.size = [YPhoto getImageSize:image];
                self.thumbImageView.center = CGPointMake(width/2, height/2);
                
                self.progressView.top = self.thumbImageView.bottom + 20;
            }];
            
            self.imageView.frame = self.thumbImageView.frame;
            
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:photo.imageUrl] placeholderImage:nil options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                self.progressView.progress = 1.0f * receivedSize / expectedSize;
            } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                self.progressView.hidden = YES;
                
                if (!error) {
                    self.thumbImageView.image = nil;
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [UIView animateWithDuration:0.3f animations:^{
                            self.imageView.size = [YPhoto getImageSize:image];
                            self.imageView.center = CGPointMake(width/2, height/2);
                        }];
                    });
                }else {
                    self.imageView.size = [YPhoto getImageSize:[UIImage imageNamed:@"chat_pic_fail"]];
                    self.imageView.image = [UIImage imageNamed:@"chat_pic_fail"];
                    self.imageView.center = CGPointMake(width/2, height/2);
                }
            }];
        }
    }
}

- (void)onTapGesture:(UITapGestureRecognizer *)tap
{
    if ([self.delegate respondsToSelector:@selector(onTapPhotoAtCell:)]) {
        [self.delegate onTapPhotoAtCell:self];
    }
}
@end
